package so.sao.code.service.web.portal;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import so.sao.code.dto.request.TagBatchFormat;
import so.sao.code.service.domain.entity.EnterpriseCustomField;
import so.sao.code.service.domain.entity.TagBatchConfig;
import so.sao.code.service.infrastructure.enums.PrintFileFieldConfigGroup;
import so.sao.code.service.infrastructure.model.vo.TagBatchConfigVO;
import so.sao.code.service.infrastructure.model.vo.TagBatchPrintFormatVO;
import so.sao.code.service.service.EnterpriseCustomFieldService;
import so.sao.code.service.service.TagBatchConfigService;
import so.sao.code.service.service.TagBatchService;
import so.sao.top.core.util.rest.RestException;

@Api(tags = "tagbatchconfig", description = "标签批次印刷格式管理")
@RestController
@RequestMapping("portal/tagbatchconfig")
public class TagBatchConfigController {

	@Autowired
	private TagBatchService tagBatchService;

	@Autowired
	private TagBatchConfigService tagBatchConfigService;

	@Autowired
	private EnterpriseCustomFieldService enterpriseCustomFieldService;

	/**
	 * 保存用户选择或设置的印刷格式
	 *
	 * @param user
	 *            当前用户对象
	 * @param tagBatchFormat
	 *            clone或配置的印刷格式
	 * @return 创建成功，返回201
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	@ApiOperation(value = "标签批次印刷格式设置接口", notes = "同时支持标快速模式和专家模式的印刷格式设置，创建成功，返回201")
	@ApiImplicitParam(name = "tagBatchFormat", value = "印刷格式参数", required = true, dataType = "TagBatchFormat", paramType = "body")
	@PostMapping
	@RequiresAuthentication
	public ResponseEntity<TagBatchConfig> printFormat(@RequestBody @Valid TagBatchFormat tagBatchFormat)
			throws IllegalAccessException, InvocationTargetException {
		TagBatchConfig config = tagBatchConfigService.saveConfig(tagBatchFormat);
		return new ResponseEntity<TagBatchConfig>(config, HttpStatus.CREATED);
	}

	/**
	 * 依据企业id查找该企业在使用中的印刷格式，如果没有查到记录，将返回404
	 *
	 * @param eseId
	 * @return List<TagBatchPrintFormat>
	 */
	@ApiOperation(value = "按企业id 标签类型查询已创建的批次即印刷格式信息接口", notes = "当用户选择克隆印刷格式时，获取可以克隆的批次列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "eseId", value = "企业ID", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "type", value = "批次类型", required = true, dataType = "int", paramType = "path") })
	@GetMapping(value = "ese/{eseId}/{type}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public List<TagBatchPrintFormatVO> findPrintFormatByEseId(@PathVariable @NotNull Long eseId,
			@PathVariable @NotNull int type) {
		return tagBatchService.findTagBatchConfigs(eseId, type);
	}

	/**
	 * 查询系统中所有设置的字段格式项
	 *
	 * @return
	 */
	@ApiOperation(value = "查询系统中所有设置的字段格式项", notes = "返回企业自定义字段及系统定义的枚举类PrintFileFieldConfig")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "eseId", value = "企业ID", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "codeType", value = "码类型", required = true, dataType = "Integer", paramType = "path") })
	@GetMapping(value = "fields/{eseId}/{codeType}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public List<List<EnterpriseCustomField>> findAllMetaConfig(@PathVariable @NotNull Long eseId,
			@PathVariable @NotNull Integer codeType) {
		List<List<EnterpriseCustomField>> result = enterpriseCustomFieldService.findFieldsInfoByEseIdAndCodeType(eseId,
				codeType);
		return result;
	}

	/**
	 * 查询系统中所有设置的字段格式组
	 *
	 * @return
	 */
	@ApiOperation(value = "查询系统中所有设置的字段格式组", notes = "返回系统定义的枚举类 PrintFileFieldConfigGroup")
	@GetMapping(value = "fieldgroup", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public PrintFileFieldConfigGroup[] findAllMetaGroupConfig() {
		return PrintFileFieldConfigGroup.values();
	}

	/**
	 * 依据id查找印刷格式，如果没有查到记录，将返回404
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "标签批次印刷格式查询接口", notes = "当用户选择克隆印刷格式时，进行查询,如果没有记录，返回404")
	@ApiImplicitParam(name = "id", value = "印刷格式ID", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public TagBatchConfigVO findPrintFormat(@PathVariable @NotNull Long id) {
		TagBatchConfigVO tagBatchConfig = tagBatchConfigService.findConfigByFormatId(id);
		if (tagBatchConfig == null) {
			throw new RestException(HttpStatus.NOT_FOUND);
		}

		return tagBatchConfig;
	}
	/**
	 * 查询系统中所有设置的字段格式项
	 *
	 * @return
	 */
	/*
	 * @ApiOperation(value = "查询系统中单码可用的字段格式项", notes =
	 * "返回企业自定义字段及系统定义的适用于单码的字段格式项，除去箱码code项")
	 * 
	 * @GetMapping(value = "singlefields/{eseId}", produces =
	 * MediaType.APPLICATION_JSON_UTF8_VALUE)
	 * 
	 * @ApiImplicitParam(name = "eseId", value = "企业ID", required = true, dataType =
	 * "Long", paramType = "path")
	 * 
	 * @RequiresAuthentication public List<EnterpriseCustomField>
	 * findAllMetaConfigExceptPackage(@PathVariable @NotNull Long eseId) {
	 * List<EnterpriseCustomField>
	 * result=enterpriseCustomFieldService.findFieldsInfoByEseId(eseId); return
	 * result; }
	 */
}
